*Recoding ZA3861 

use "Data\ZA3861\ZA3861_v3-0-0.dta" , clear

*Support for democracy
gen $idea_democracy				= v86	

*define missing values
mvdecode $idea_democracy, mv(9=.a\8=.b)  //.a = no answer; .b= = don't know)

*reverse scale
replace $idea_democracy = (6-idea_democracy)/5 if !missing($idea_democracy)	 

*Support for liberal democracy
gen $libdem_opposition			= v170k
gen $libdem_freeopinion			= v170h
gen $libdem_partyopportunity	= v170o
* libdem_criticism libdem_conflict libdem_generalwill not asked

*Vertrauen Institutionen
gen $trust_parties = v220h
gen $trust_bundestag = v220a
gen $trust_government = v220c
gen $trust_verfassungsgericht = v220b
gen $trust_justice = v220d

*Efficacy
gen $ego_politicdiscuss = v170i
gen $ego_politicscomplicated = v170d



*define missing values
mvdecode $libdem_opposition $libdem_freeopinion $libdem_partyopportunity ///
		 $trust_parties $trust_bundestag $trust_government $trust_verfassungsgericht $trust_justice $ego_politicdiscuss $ego_politicscomplicated, ///
	mv(9=.a\8=.b)  //.a = no answer; .b= = don't know)


*Standardize range from 0 to 1; higher values indicate higher support for democracy
*reverse scale if necessary and standardize

foreach var of varlist $libdem_opposition $libdem_freeopinion $libdem_partyopportunity {
	replace `var'	= (`var'-1)/4 if !missing(`var')
}

foreach var of varlist $trust_parties $trust_bundestag $trust_government $trust_verfassungsgericht $trust_justice {
	replace `var' = (`var' - 1)/4 if !missing(`var')
}

foreach var of varlist $ego_politicdiscuss {
	replace `var' = (`var' - 1)/4 if !missing(`var')
}

foreach var of varlist $ego_politicscomplicated {
	replace `var' = (5 - `var')/4 if !missing(`var')
}


*Sociodemographic Controls
gen			$gender 			= vsex
*define missing values
mvdecode	$gender, mv(9=.a)  //.a = no answer
replace 	$gender				= $gender-1 			if !missing($gender)


gen 		$age				= 2002-vjahr			if vjahr<9999			//survey year - year of birth; 9999 == no answer	
*define missing values
replace $age				 	= .a					if $age == .			//.a = no answer

gen 		$education			= vbildga
*define missing values
mvdecode	$education, mv(9=.a)  //.a = no answer
replace 	$education 			= 0 if $education 	<= 4 & !missing($education) // Still in School, none, Hautpschule, Mittlere Reife, = 0
replace 	$education 			= 1 if $education 	>=5  & !missing($education) // Abitur/EOS; university = 1

gen $eastwest 					= vsplitwo-1

*Weight
gen weight = vgwow1

*Party ID
*Did R lean towards  a left, right or other party?
*1: right, (CDU/CSU, FDP, AfD), 0: left (Left/PDS, Greens, SPD), -1: other
gen $partyid_category		= 1 	if inlist(vpid, 1, 2, 4)
replace $partyid_category	= 0 	if inlist(vpid, 3, 5, 7)
replace $partyid_category	= -1 	if inlist(vpid, 6, 9)



*Did R lean towards  a left, right or other party?
*1: right, (CDU/CSU, FDP, AfD), 0: left (Left/PDS, Greens, SPD), -1: other
gen $partyvote_category		= 1 	if inlist(v70, 1, 4)
replace $partyvote_category	= 0 	if inlist(v70, 2, 3, 5)
replace $partyvote_category	= -1 	if inlist(v70, 6, 7, 8, 9, 10, 11)


*Meta variables
gen $dataset 					= "ZA3861"
gen $year 						= 2002

*check
tab $idea_democracy v86, m
tab $libdem_opposition v170k, m
tab $libdem_freeopinion v170h, m
tab $libdem_partyopportunity v170o, m
tab $gender vsex, m
tab $age, m
tab $education vbildga, m
tab $eastwest vsplitwo, m

*Save recode dataset
keep $idea_democracy $libdem_opposition	$libdem_freeopinion 					///
	$trust_parties $trust_bundestag $trust_government $trust_verfassungsgericht $trust_justice $ego_politicdiscuss $ego_politicscomplicated ///
	$libdem_partyopportunity $gender $age $education $eastwest $dataset $year  weight	$partyid_category	$partyvote_category	
save ZA3861_recoded.dta, replace

